﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Globalization;

using CE.Global;
using CE.Database;

public partial class cmi_default : MyLanguageBasePage
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
            ddlVehicleType_SelectedIndexChanged(sender, e);
            ddlSelectInsurer_SelectedIndexChanged(sender, e);
            LoadManufactYearList();
            ClearAllList();

            CultureInfo ci = CultureInfo.CurrentCulture;

            DateTime dtNow = new DateTime();
            dtNow = General.GetUTCDateTime();
            for (int i = 2; i < 61; i++)
            {
                DateTime dtTemp = dtNow.AddDays(i);
                string strDate = dtTemp.Date.Day.ToString("00") + dtTemp.Date.Month.ToString("00") + dtTemp.Date.Year.ToString("0000");
                ddlCoverageComDate.Items.Add(new ListItem(dtTemp.ToString("dd/MMMM/yyyy", ci), strDate));
            }
            ddlCoverageComDate.Items.Insert(0, new ListItem(Resources.CarInsurance.Select, "-1"));
        }
    }

    

    protected void btnNext_Click(object sender, EventArgs e)
    {
        Page.Validate("cmi");
        if (Page.IsValid)
        {
            float _fAmount = Convert.ToSingle(ddlSelectPrice.SelectedItem.Text);

            if (ddlShipMethod.Text == "2")
            {
                _fAmount += 37.00f;
            }

            //float _fAmount = Convert.ToSingle(txtPremium.Text);
            string _strAmount, eAmount, _strOrderRefNo, eOrderRefNo;
            _strAmount = ""; eAmount = ""; _strOrderRefNo = ""; eOrderRefNo = "";
            string key = "cxDAtravel";
            CXDecryption.Decryption cxd = new CXDecryption.Decryption();
            _strAmount = _fAmount.ToString();
            _strOrderRefNo = General.GetUTCDateTime().ToString("yyyymmddhhmmss");
            if (_strAmount.ToString() != "" && _strAmount.ToString() != null)
            {
                byte[] Amount = cxd.Encrypt(_strAmount, key);
                eAmount = Convert.ToBase64String(Amount);
            }
            if (_strOrderRefNo.ToString() != "" && _strOrderRefNo.ToString() != null)
            {
                byte[] OrderRefNo = cxd.Encrypt(_strOrderRefNo, key);
                eOrderRefNo = Convert.ToBase64String(OrderRefNo);
            }
            string URL = ConfigurationManager.AppSettings["TH.BKKPaymentPage"].ToString();
            URL += "?pa=" + eAmount + "&orno=" + eOrderRefNo;
            //string URL = "http://203.142.18.110/bkk_test/MethodOne.aspx?pa=" + eAmount + "&orno=" + eOrderRefNo;
            Response.Redirect(URL);
        }
    }
    protected void ddlVehicleType_SelectedIndexChanged(object sender, EventArgs e)
    {
        ddlSelectInsurer.Items.Clear();
        ddlSelectPrice.Items.Clear();

        if (ddlVehicleType.Text == "1")
        {
            lblNormalPriceAmount.Text = "645.21";
            ddlSelectInsurer.Items.Add(new ListItem("LMG Insurance - ฿ 590.00 - Save 55.21", "29"));
            ddlSelectInsurer.Items.Add(new ListItem("Chartis Insurance - ฿ 600.00 - Save 45.21", "30"));
            ddlSelectInsurer.Items.Add(new ListItem("MSIG Insurance - ฿ 580.00 - Save 65.21", "31"));
            ddlSelectInsurer.Items.Add(new ListItem("Alliance CP Insurance - ฿ 595.00 - Save 50.21", "32"));

            ddlSelectPrice.Items.Add(new ListItem("590.00", "101"));
            ddlSelectPrice.Items.Add(new ListItem("600.00", "102"));
            ddlSelectPrice.Items.Add(new ListItem("580.00", "103"));
            ddlSelectPrice.Items.Add(new ListItem("595.00", "104"));
        }
        else if (ddlVehicleType.Text == "2")
        {
            lblNormalPriceAmount.Text = "967.28";
            ddlSelectInsurer.Items.Add(new ListItem("LMG Insurance - ฿ 890.00 - Save 77.28", "29"));
            ddlSelectInsurer.Items.Add(new ListItem("Chartis Insurance - ฿ 900.00 - Save 67.28", "30"));
            ddlSelectInsurer.Items.Add(new ListItem("MSIG Insurance - ฿ 880.00 - Save 87.28", "31"));
            ddlSelectInsurer.Items.Add(new ListItem("Alliance CP Insurance - ฿ 895.00 - Save 72.28", "32"));

            ddlSelectPrice.Items.Add(new ListItem("890.00", "201"));
            ddlSelectPrice.Items.Add(new ListItem("900.00", "202"));
            ddlSelectPrice.Items.Add(new ListItem("880.00", "203"));
            ddlSelectPrice.Items.Add(new ListItem("895.00", "204"));
        }
        else if (ddlVehicleType.Text == "3")
        {
            lblNormalPriceAmount.Text = "1,182.35";
            ddlSelectInsurer.Items.Add(new ListItem("LMG Insurance - ฿ 1090.00 - Save 92.35", "29"));
            ddlSelectInsurer.Items.Add(new ListItem("Chartis Insurance - ฿ 1080.00 - Save 102.35", "30"));
            ddlSelectInsurer.Items.Add(new ListItem("MSIG Insurance - ฿ 1060.00 - Save 122.35", "31"));
            ddlSelectInsurer.Items.Add(new ListItem("Alliance CP Insurance - ฿ 1070.00 - Save 112.35", "32"));

            ddlSelectPrice.Items.Add(new ListItem("1090.00", "301"));
            ddlSelectPrice.Items.Add(new ListItem("1080.00", "302"));
            ddlSelectPrice.Items.Add(new ListItem("1060.00", "303"));
            ddlSelectPrice.Items.Add(new ListItem("1070.00", "304"));
        }

        ddlSelectInsurer_SelectedIndexChanged(sender, e);
    }

    protected void ddlSelectInsurer_SelectedIndexChanged(object sender, EventArgs e)
    {
        ddlSelectPrice.SelectedIndex = ddlSelectInsurer.SelectedIndex;
        float _normalPrice = Convert.ToSingle(lblNormalPriceAmount.Text.Replace(",", ""));
        float _discountPrice = Convert.ToSingle(ddlSelectPrice.SelectedItem.Text);

        lblSavePrice.Text = "You save: ฿  " + (_normalPrice - _discountPrice).ToString("#0.00");
        ddlShipMethod_SelectedIndexChanged(sender, e);
    }

    private void LoadManufactYearList()
    {
        int _yearCount = 10;
        ddlManufactYear.Items.Clear();

        int _startYear = General.GetUTCDateTime().Year - _yearCount;
        int _endYear = General.GetUTCDateTime().Year;
        for (int i = _endYear; i >= _startYear; i--)
        {
            //ddlManufactYear.Items.Add(new ListItem(i.ToString(), i.ToString()));
            if (CultureInfo.CurrentCulture.Name != "th-TH")
            {
                ddlManufactYear.Items.Add(new ListItem(i.ToString(), i.ToString()));
            }
            else
            {
                ddlManufactYear.Items.Add(new ListItem(i.ToString() + " - พ.ศ. " + (i + 543).ToString(), i.ToString()));
            }
        }

        ddlManufactYear.Items.Insert(0, new ListItem(Resources.CarInsurance.Select, "-1"));
    }

    protected void ddlCarMake_SelectedIndexChanged(object sender, EventArgs e)
    {
        //System.Threading.Thread.Sleep(10000);
        if (ddlCarMake.Text == "-1")
        {
            ddlModelBodyCCDoor.Items.Clear();
            ddlModelBodyCCDoor.Items.Add(new ListItem(Resources.CarInsurance.Select, "-1"));

            txtSumInsured.Text = "0";
            lblSumInsured.Text = "0";
            txtGroupNo.Text = "-1";
        }
        else
        {
            GetCarModelDescList(ddlCarMake.Text, ddlManufactYear.Text, ddlModelBodyCCDoor);
            if (ddlModelBodyCCDoor.Items.Count > 0)
            {
                ddlModelBodyCCDoor.SelectedIndex = 0;
                ddlModelBodyCCDoor_SelectedIndexChanged(sender, e);
            }
        }
    }

    private void ClearAllList()
    {
        ddlCarMake.Items.Clear();
        ddlCarMake.Items.Add(new ListItem(Resources.CarInsurance.Select, "-1"));
        ddlModelBodyCCDoor.Items.Clear();
        ddlModelBodyCCDoor.Items.Add(new ListItem(Resources.CarInsurance.Select, "-1"));
        txtSumInsured.Text = "0";
        lblSumInsured.Text = "0";
        txtGroupNo.Text = "-1";
    }

    private void GetCarModelDescList(string CarMake, string Year, DropDownList ddlList)
    {
        MyDatabase _db = new MyDatabase();
        DataTable _dt = new DataTable("tblModelDesc");
        SortedList _spList = new SortedList();
        _spList.Add("@CarMake", CarMake);
        _spList.Add("@Year", Year);
        _dt = _db.GetDataTableBySP("sprocGetCarModelDescTHListByCarMakeCode", _spList);
        ddlList.Items.Clear();
        ddlModelBodyCCDoor.Items.Clear();
        ddlModelBodyCCDoor.Items.Add(new ListItem(Resources.CarInsurance.Select, "-1"));
        if (_dt != null && _dt.Rows.Count > 0)
        {
            for (int i = 0; i < _dt.Rows.Count; i++)
            {
                ddlList.Items.Add(new ListItem(_dt.Rows[i][0].ToString()));
            }
        }
        else
        {
            ddlList.Items.Insert(0, new ListItem(Resources.CarInsurance.Select, "-1"));
        }
        _dt = null;
        _db = null;
    }

    private void GetCarMakeList(string Year, DropDownList ddlList)
    {
        MyDatabase _db = new MyDatabase();
        DataTable _dt = new DataTable("tblMake");
        SortedList _spList = new SortedList();
        _spList.Add("@Year", Year);
        _dt = _db.GetDataTableBySP("sprocGetCarMakeTHListByYear", _spList);
        ddlList.Items.Clear();
        ddlList.Items.Insert(0, new ListItem(Resources.CarInsurance.Select, "-1"));
        //ddlCarMake.Items.Add(new ListItem("Test", "TEST"));
        if (_dt != null && _dt.Rows.Count > 0)
        {
            for (int i = 0; i < _dt.Rows.Count; i++)
            {
                ddlList.Items.Add(new ListItem(_dt.Rows[i][1].ToString(), _dt.Rows[i][0].ToString()));
            }
        }

        _dt = null;
        _db = null;
    }

    protected void ddlManufactYear_SelectedIndexChanged(object sender, EventArgs e)
    {
        if (ddlManufactYear.Text == "-1")
        {
            ClearAllList();
        }
        else
        {
            GetCarMakeList(ddlManufactYear.Text, ddlCarMake);
            if (ddlCarMake.Items.Count > 0)
            {
                ddlCarMake.SelectedIndex = 0;
                ddlCarMake_SelectedIndexChanged(sender, e);
            }
        }
    }


    protected void ddlModelBodyCCDoor_SelectedIndexChanged(object sender, EventArgs e)
    {
        if (ddlManufactYear.SelectedIndex >= 0 && ddlCarMake.SelectedIndex >= 0 & ddlModelBodyCCDoor.SelectedIndex >= 0)
        {
            string[] _tempDesc = ddlModelBodyCCDoor.Text.Split(' ');
            string _model = "";
            string _body = "";
            string _cc = "";
            string _door = "";
            if (_tempDesc.Length == 5)
            {
                _model = _tempDesc[0];
                _body = _tempDesc[1];
                _cc = _tempDesc[2].Substring(0, _tempDesc[2].Length - 2);
                _door = _tempDesc[3];
            }
            else if (_tempDesc.Length == 6)
            {
                _model = _tempDesc[0] + " " + _tempDesc[1];
                _body = _tempDesc[2];
                _cc = _tempDesc[3].Substring(0, _tempDesc[3].Length - 2);
                _door = _tempDesc[4];
            }
            else if (_tempDesc.Length == 7)
            {
                _model = _tempDesc[0] + " " + _tempDesc[1] + " " + _tempDesc[2];
                _body = _tempDesc[3];
                _cc = _tempDesc[4].Substring(0, _tempDesc[4].Length - 2);
                _door = _tempDesc[5];
            }
            lblModelDesc.Text = "model : " + _model + ", body: " + _body + ", cc: " + _cc + ", door: " + _door;
            GetCarModelSumInsuredAmountAndVehicleType(ddlCarMake.Text, _model, _body, _cc, _door, ddlManufactYear.Text, txtSumInsured, lblSumInsured, lblCarVehicleType);
            txtNewSumInsured.Text = txtSumInsured.Text;

            //ChangeVehicleUsageList(lblCarVehicleType.Text);
            GetCarModelGroupNo(ddlCarMake.Text, _model, _body, _cc, _door, txtGroupNo);
            //ScriptManager.RegisterClientScriptBlock(Page, this.GetType(), "Init_JTip_Driver", "JT_init();", true);
            //ScriptManager.RegisterClientScriptBlock(Page, this.GetType(), "ShowHide_AccMod", "CheckAccMod();", true);
            //CheckAccMod()
        }
    }

    private void GetCarModelSumInsuredAmountAndVehicleType(string CarMake, string CarModel, string Body, string CC, string Door, string Year, TextBox txtTextBox, Label lblLabel, Label lblLabelTwo)
    {
        MyDatabase _db = new MyDatabase();
        DataTable _dt = new DataTable("tblModelBody");
        SortedList _spList = new SortedList();
        _spList.Add("@CarMake", CarMake);
        _spList.Add("@CarModel", CarModel);
        _spList.Add("@Body", Body);
        _spList.Add("@CC", CC);
        _spList.Add("@Door", Door);
        _spList.Add("@Year", Year);
        _dt = _db.GetDataTableBySP("sprocGetCarModelSumInsuredAndVehicleTypeTHListByYear", _spList);
        if (_dt != null && _dt.Rows.Count > 0)
        {
            txtTextBox.Text = _dt.Rows[0][0].ToString();
            lblLabelTwo.Text = _dt.Rows[0][1].ToString();
            lblLabel.Text = Convert.ToInt32(txtTextBox.Text).ToString("#,000");
            //txtTextBox.Text = string.Format("{0:#,0}", _dt.Rows[0][0]);
        }
        else
        {
            txtTextBox.Text = "0";
            lblLabel.Text = "0";
        }
        txtTextBox.ReadOnly = true;
        _dt = null;
        _db = null;
    }

    private void GetCarModelGroupNo(string CarMake, string CarModel, string Body, string CC, string Door, TextBox txtTextBox)
    {
        MyDatabase _db = new MyDatabase();
        DataTable _dt = new DataTable("tblModelBody");
        SortedList _spList = new SortedList();
        _spList.Add("@CarMake", CarMake);
        _spList.Add("@CarModel", CarModel);
        _spList.Add("@Body", Body);
        _spList.Add("@CC", CC);
        _spList.Add("@Door", Door);
        _dt = _db.GetDataTableBySP("sprocGetCarModelGroupTH", _spList);

        if (_dt != null && _dt.Rows.Count > 0)
        {
            txtTextBox.Text = _dt.Rows[0][0].ToString();
        }
        else
        {
            txtGroupNo.Text = "-1";
        }
        txtTextBox.ReadOnly = true;
        _dt = null;
        _db = null;
    }

    protected void chkAsInsured_CheckedChanged(object sender, EventArgs e)
    {
        txtShipName.ReadOnly = false;
        txtShipAddress.ReadOnly = false;
        rfvShipName.Enabled = true;
        rfvShipAddress.Enabled = true;

        if (chkAsInsured.Checked)
        {
            txtShipName.Text = txtFullName.Text;
            txtShipAddress.Text = txtInsuredAddress.Text;

            txtShipName.ReadOnly = true;
            txtShipAddress.ReadOnly = true;

            rfvShipName.Enabled = false;
            rfvShipAddress.Enabled = false;
        }
    }

    protected void ddlShipMethod_SelectedIndexChanged(object sender, EventArgs e)
    {
        float _fAmount = Convert.ToSingle(ddlSelectPrice.SelectedItem.Text);

        if (ddlShipMethod.Text == "2")
        {
            _fAmount += 37.00f;
            lblTotalAmountDue.Visible = true;
            if (_fAmount >= 1000)
            {
                lblTotalAmountDue.Text = "Total Price: ฿ " + _fAmount.ToString("#,000.00");
            }
            else
            {
                lblTotalAmountDue.Text = "Total Price: ฿ " + _fAmount.ToString("#.00");
            }
        }
        else
        {
            lblTotalAmountDue.Visible = false;
        }
    }
}